﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging

Namespace Arwani
    Partial Class Backend_AddDivision
        Inherits System.Web.UI.Page

        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            If Me.txtDivisionName.Text = "" Then Me.lblError.Text = "Please enter division name" : Exit Sub
            Dim arwCon As New ArwaniController
            Dim strQ As String
            Dim strBackgroundName As String = ""
            Dim strHeaderImageName As String = ""
            Dim strMainBannerName As String = ""
            Dim strFlashBanner As String = ""

            If Request.QueryString("divid") = "" Then
                If Me.flBackgroundImage.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.flBackgroundImage.PostedFile.FileName)
                    strBackgroundName = "Background_" & Guid.NewGuid.ToString() & ext
                    Me.flBackgroundImage.PostedFile.SaveAs(Server.MapPath("../Divisions/" & strBackgroundName))
                End If
                'If Me.flHeaderImage.PostedFile.FileName <> "" Then
                '    Dim ext As String = IO.Path.GetExtension(Me.flHeaderImage.PostedFile.FileName)
                '    strHeaderImageName = "Header_" & Guid.NewGuid.ToString() & ext
                '    Me.flHeaderImage.PostedFile.SaveAs(Server.MapPath("../Divisions/" & strHeaderImageName))
                'End If
                If Me.flMainBannerImage.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.flMainBannerImage.PostedFile.FileName)
                    strMainBannerName = "Banner_" & Guid.NewGuid.ToString() & ext
                    Me.flMainBannerImage.PostedFile.SaveAs(Server.MapPath("../Divisions/" & strMainBannerName))
                End If
                If Me.flFlashBanner.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.flFlashBanner.PostedFile.FileName)
                    strFlashBanner = "Flash_" & Guid.NewGuid.ToString() & ext
                    Me.flFlashBanner.PostedFile.SaveAs(Server.MapPath("../Flash/" & strFlashBanner))
                End If

                If Me.fileImage.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.fileImage.PostedFile.FileName)
                    Dim strFileName As String = Guid.NewGuid.ToString() & ext
                    Me.fileImage.PostedFile.SaveAs(Server.MapPath("../Divisions/" & strFileName))
                    Call ResizeImages(strFileName, "Thumb_" & strFileName)
                    strQ = "insert into  division (divisiontitle,divisionpic,backgroundimage,header_image,main_banner,flash) values ('" & Me.txtDivisionName.Text & "','" & strFileName & "','" & strBackgroundName & "','" & strHeaderImageName & "','" & strMainBannerName & "'," & strFlashBanner & "')"
                Else
                    strQ = "insert into  division (divisiontitle,backgroundimage,header_image,main_banner,flash) values ('" & Me.txtDivisionName.Text & "','" & strBackgroundName & "','" & strHeaderImageName & "','" & strMainBannerName & "'," & strFlashBanner & "')"
                End If
                arwCon.ExecuteNonQuery(strQ)
            Else
                If Me.fileImage.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.fileImage.PostedFile.FileName)
                    Dim strFileName As String = Guid.NewGuid.ToString() & ext
                    Me.fileImage.PostedFile.SaveAs(Server.MapPath("../Divisions/" & strFileName))
                    Call ResizeImages(strFileName, "Thumb_" & strFileName)

                    strQ = "update division set divisiontitle='" & Me.txtDivisionName.Text & "',divisionpic='" & strFileName & "' where divisionid=" & CInt(Request.QueryString("divid"))
                    arwCon.ExecuteNonQuery(strQ)
                End If

                If Me.flBackgroundImage.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.flBackgroundImage.PostedFile.FileName)
                    strBackgroundName = "Background_" & Guid.NewGuid.ToString() & ext
                    Me.flBackgroundImage.PostedFile.SaveAs(Server.MapPath("../Divisions/" & strBackgroundName))
                    strQ = "update division set backgroundimage='" & strBackgroundName & "' where divisionid=" & CInt(Request.QueryString("divid"))
                    arwCon.ExecuteNonQuery(strQ)
                End If
                'If Me.flHeaderImage.PostedFile.FileName <> "" Then
                '    Dim ext As String = IO.Path.GetExtension(Me.flHeaderImage.PostedFile.FileName)
                '    strHeaderImageName = "Header_" & Guid.NewGuid.ToString() & ext
                '    Me.flHeaderImage.PostedFile.SaveAs(Server.MapPath("../Divisions/" & strHeaderImageName))
                '    strQ = "update division set header_image='" & strHeaderImageName & "' where divisionid=" & CInt(Request.QueryString("divid"))
                '    arwCon.ExecuteNonQuery(strQ)
                'End If
                If Me.flMainBannerImage.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.flMainBannerImage.PostedFile.FileName)
                    strMainBannerName = "Banner_" & Guid.NewGuid.ToString() & ext
                    Me.flMainBannerImage.PostedFile.SaveAs(Server.MapPath("../Divisions/" & strMainBannerName))
                    strQ = "update division set main_banner='" & strMainBannerName & "' where divisionid=" & CInt(Request.QueryString("divid"))
                    arwCon.ExecuteNonQuery(strQ)
                End If

                If Me.flFlashBanner.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.flFlashBanner.PostedFile.FileName)
                    strFlashBanner = "Flash_" & Guid.NewGuid.ToString() & ext
                    Me.flFlashBanner.PostedFile.SaveAs(Server.MapPath("../Flash/" & strFlashBanner))
                    strQ = "update division set flash='" & strFlashBanner & "' where divisionid=" & CInt(Request.QueryString("divid"))
                    arwCon.ExecuteNonQuery(strQ)
                End If
            End If
            Response.Redirect("ManageDivision.aspx")
        End Sub

        Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            Response.Redirect("ManageDivision.aspx")
        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then
                If Page.IsPostBack = True Then Exit Sub
                If Request.QueryString("divid") <> "" Then
                    Call loadData()
                End If

            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub
        Public Sub loadData()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from division where divisionid=" & CInt(Request.QueryString("divid"))
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                If Not IsDBNull(dtTable.Rows(0)("divisiontitle")) Then Me.txtDivisionName.Text = dtTable.Rows(0)("divisiontitle")
            End If
        End Sub
        Sub ResizeImages(ByVal FileName, ByVal NewFileName)
            Dim intWidth, intHeight As Integer

            intWidth = 730 '*** Fix Width ***'  
            intHeight = 248   '*** If = 0 Auto Re-Cal Size ***'  

            Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("../Divisions/" & FileName))

            Dim objBitmap As Bitmap
            '*** Calculate Height ***'  
            If intHeight > 0 Then
                objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
            Else
                If objGraphic.Width > intWidth Then
                    Dim ratio As Double = objGraphic.Height / objGraphic.Width
                    intHeight = ratio * intWidth
                    objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
                Else
                    objBitmap = New Bitmap(objGraphic)
                End If
            End If

            '*** Save As  ***'  
            objBitmap.Save(Server.MapPath("../Divisions/" & NewFileName), objGraphic.RawFormat)

            '*** Close ***'  
            objGraphic.Dispose()

            '*** Nothing ***'  
            objBitmap = Nothing
            objGraphic = Nothing

        End Sub
    End Class
End Namespace